package com.jgsu.farm.mapper.sql;

import com.jgsu.farm.entity.Plant;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface PlantMapperSql {
    @Update("update plant set chineseName = #{chinesename},latinName = #{latinname},alias = #{alias},category = #{category},distribution = #{distribution},morphology = #{morphology},plantImage = #{plantimage},growthHabit = #{growthhabit} where plantID = #{plantid}")
    void updatePlantMsg(Integer plantid, String latinname, String chinesename, String alias, Integer category, String distribution, String morphology, Integer plantimage, String growthhabit);

    @Select("select plantID from plant where chineseName = #{chinesename}")
    Integer findId(String chinesename);

    @Update("update plant set chineseName = #{chinesename} where plantID = #{plantid}")
    void updateNameById(Integer plantid);

    @Select("select * from plant where plantID = #{plantid}")
    Plant selectPlantById(Integer plantid);

    @Delete("delete from plant where plantId = #{plantid}")
    int deleteById(Integer plantid);

    @Results(id = "plantMap", value = {
            @Result(id = true, column = "plantid", property = "plantid"),
            @Result(column = "chinesename", property = "chinesename"),
            @Result(column = "latinname", property = "latinname"),
            @Result(column = "alias", property = "alias"),
            @Result(column = "category", property = "category"),
            @Result(column = "category",property = "biologyCategory",one = @One(select = "com.jgsu.farm.mapper.sql.BiologyMapperSql.selectCategoryById",fetchType = FetchType.EAGER)),
            @Result(column = "distribution", property = "distribution"),
            @Result(column = "morphology", property = "morphology"),
            @Result(column = "plantimage", property = "plantimage"),
            @Result(column = "plantimage",property = "matchImageList",many = @Many(select = "com.jgsu.farm.mapper.sql.MatchImageMapperSql.findMatchImageByPlantId",fetchType = FetchType.LAZY)),
            @Result(column = "growthhabit", property = "growthhabit"),
    })
    @Select("select * from plant")
    List<Plant> selectPlantListWithBiologyCategory();
}
